package com.easyattendance.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.StyledEditorKit.BoldAction;

import com.easyattendance.model.StudentLeave;
import com.easyattendance.services.StudentLeaveService;
//import com.easyattendance.nls.UseProperties;

@WebServlet(name = "StudentLeaveServlet", urlPatterns = { "/StudentLeaveServlet" })
public class StudentLeaveServlet extends HttpServlet {
	
	private static final long serialVersionUID = 2079072999537242992L;

	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {  
    	response.setContentType("text/html;charset=UTF-8");
		
    	int student_roll_no = 0;
        if(!request.getParameter("student_roll_no").isEmpty()){
        	student_roll_no = Integer.parseInt(request.getParameter("student_roll_no"));
        }
		String classId = request.getParameter("student_classid");
		String leave_reason=request.getParameter("reason_for_leave");
		String leave_dur = request.getParameter("leave_duration");
		String st_dt = request.getParameter("leave_start_date");
		String end_dt = request.getParameter("leave_end_date");
        Date leave_date = null,leave_start_date = null,leave_end_date = null;
        java.sql.Date start_date_sql = null,end_date_sql = null;
		boolean dispatcherFlag = false;
        if(student_roll_no < 1 || leave_reason.isEmpty() || leave_dur == null || st_dt.isEmpty()){
        	request.setAttribute("null_values", "All * fields are required");
        	dispatcherFlag = true;
        }else{
        	if(leave_dur.equals("MoreThanOneDay") && end_dt.isEmpty()){
        		request.setAttribute("null_values", "All * fields are required");
        		dispatcherFlag = true;
        	}
        }
        if(!dispatcherFlag){
        
	        try {
			    leave_start_date = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("leave_start_date"));
			    start_date_sql=new java.sql.Date(leave_start_date.getTime());
			    
			    String end_date=request.getParameter("leave_end_date");
			    if(end_date!=null)	
			    {
			    	leave_end_date = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("leave_end_date"));
			    	end_date_sql=new java.sql.Date(leave_end_date.getTime());
			    }
			    else if(end_date==null)
			    {
			    	end_date_sql=start_date_sql;
			    }			   
			    
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}        
        
	        StudentLeaveService LeaveService = new StudentLeaveService();
        
            boolean result=false;  
        	StudentLeave leave = new StudentLeave(student_roll_no,classId,start_date_sql,end_date_sql,leave_reason);
            result = LeaveService.addStudentLeave(leave);

        	if (result) {
        		request.setAttribute("add_success", "Leave recorded successfully.");
			} else {
				request.setAttribute("add_unsuccess", "Leave could not be recorded.");				
			}
        	RequestDispatcher rd = request.getRequestDispatcher("views/teacher/manageStudentLeave.jsp");
	    	rd.forward(request, response);
        }
        if(dispatcherFlag) {
			RequestDispatcher rd = request.getRequestDispatcher("views/teacher/manageStudentLeave.jsp");
	    	rd.forward(request, response);
		}
    }

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
